package com.sc.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sc.admin.common.annotation.Log;
import com.sc.admin.common.enums.BusinessTypeEnum;
import com.sc.admin.common.enums.ModuleEnum;
import com.sc.admin.common.result.Result;
import com.sc.admin.common.util.ObjUtils;
import com.sc.admin.model.form.SysConfigForm;
import com.sc.admin.model.vo.SysConfigVO;
import com.sc.admin.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.sc.admin.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *@Author shen chen
 *@Date 2024-06-28
 */
@Tag(name = "10.系统配置")
@RestController
@RequestMapping("/api/v1/config")
@RequiredArgsConstructor
public class SysConfigController {
    private final SysConfigService sysConfigService;

    @Operation(summary = "查询配置列表")
    @GetMapping
    public Result<List<SysConfigVO>> query() {
        return Result.success(ObjUtils.copyList(sysConfigService.query().list(), SysConfigVO.class));
    }

    @Operation(summary = "新增配置")
    @PostMapping
    @SaCheckPermission("sys:sys-config:add")
    @PreventDuplicateSubmit
    @Log(module = ModuleEnum.SYS_CONFIG, title = "新增配置", businessType = BusinessTypeEnum.INSERT)
    public Result<Integer> saveConfig(@Valid @RequestBody SysConfigForm formData) {
        Integer id = sysConfigService.saveConfig(formData);
        return Result.success(id);
    }

    @Operation(summary = "修改配置")
    @PutMapping(value = "/{id}")
    @SaCheckPermission("sys:sys-config:edit")
    @Log(module = ModuleEnum.SYS_CONFIG, title = "修改配置", businessType = BusinessTypeEnum.UPDATE)
    public Result<Void> updateConfig(@PathVariable Integer id, @Valid @RequestBody SysConfigForm formData) {
        sysConfigService.updateConfig(id, formData);
        return Result.success();
    }

    @Operation(summary = "关闭/开启 开关")
    @PutMapping(value = "/{id}/{configSwitch}")
    @SaCheckPermission("sys:sys-config:switch")
    @Log(module = ModuleEnum.SYS_CONFIG, title = "关闭/开启 开关", businessType = BusinessTypeEnum.UPDATE)
    public Result<Void> openOrClose(@PathVariable Integer id, @Valid @PathVariable String configSwitch) {
        sysConfigService.openOrClose(id, configSwitch);
        return Result.success();
    }


}
